package com.android.ClientApp;

import com.java.serverpackage.MessagePacket;
import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ClientAppActivity extends Activity implements OnClickListener {
    private EditText inputBox;
    private TextView receivedMsgBox;
   
    protected static final int DATA_MESSAGE    = 0;
    protected static final int PLAYER_JOINED   = 1;
    protected static final int PLAYER_QUIT     = 2;
    protected static final int JOIN_SUCCESS    = 3;
	protected static final int RECEIVE_HISTORY = 4;
	

	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
     
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        Button sendButton = (Button)findViewById(R.id.sendButton);
        sendButton.setOnClickListener(this);
        
        Button networkOp = (Button)findViewById(R.id.networkOp);
        networkOp.setOnClickListener(this);
        
        receivedMsgBox = (TextView)findViewById(R.id.receivedMsgBox);
        inputBox = (EditText)findViewById(R.id.inputDataField);
        
        new ReceiveTask().execute();
        
    }
    
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.sendButton:
			//Test message
			String msg = inputBox.getText().toString();
			NetworkHandling.writeToServer(new MessagePacket(0,msg));
			// Add if cases to determine if we should write via bluetooth or tcp
			break;
		case R.id.networkOp:
			startActivity(new Intent(this,NetworkOptions.class));
			break;
		}
	}

	private class ReceiveTask extends AsyncTask<Void, MessagePacket, Void> {		
		
		@Override
		protected Void doInBackground(Void... param) {
			android.os.Debug.waitForDebugger();
			MessagePacket msg = null;
			while(true) {
				msg = NetworkHandling.readFromServer();
				if(msg != null)
					publishProgress(msg);
			}
		}
	
		@Override
		protected void onProgressUpdate(MessagePacket... msg) {
			
			switch(msg[0].getId()) {
			case DATA_MESSAGE:
				createToastMsg("Message: " + msg[0].getMsg());
				//TODO handle the coordinates for painting
				break;
			case PLAYER_JOINED:
				createToastMsg("A new player has joined the game.\n" + "Members: "+ msg[0].getMembers());
				//receivedMsgBox.setText("A new player has joined the game.\n" + "Members: "+ msg[0].getMembers());
				break;
			case PLAYER_QUIT:
				createToastMsg("A player has left the game.\n" + "Members: "+ msg[0].getMembers());
			//	receivedMsgBox.setText("A player has left the game.\n" + "Members: "+ msg[0].getMembers());
				break;
			case JOIN_SUCCESS:
				createToastMsg("You have successfully joined a room.\n" + "Members: "+ msg[0].getMembers());
			//	receivedMsgBox.setText("You have successfully joined a room.\n" + "Members: "+ msg[0].getMembers());
				break;
			case RECEIVE_HISTORY:
				Iterator<MessagePacket> it = msg[0].getHistory().iterator();
				
				while(it.hasNext()) {
						createToastMsg("Message: " + it.next().getMsg());		
				}
				break;
			default:
				receivedMsgBox.setText("SUCK MYYYY DICK EDDIII");
				break;
			}	
		}
		
		protected void createToastMsg(String textmsg){
			Toast m = Toast.makeText(getApplicationContext(), textmsg, 3500);
			m.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
			m.show();
		}
			
	}
	
}
